#include <fcntl.h>
#include <sys/uio.h>
#import <unistd.h>

int main()
{
    const char* filename = "/tmp/stringfile.txt";
    int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR );

    if (fd == -1)
    {
        printf("error\n");
        return 2 ;
    }
    const char text[] = "text for scatter/gather io";

    int l = strlen(text);
    struct iovec v[2];
    v[0].iov_base = &l;
    v[0].iov_len = sizeof(l);
    v[1].iov_base = text;
    v[1].iov_len = l;

    size_t r = writev(fd, v, 2);

    if (r == -1)
    {
        printf("error\n");
        return 2;
    }

    close(fd);

    fd = open (filename, O_RDONLY);

    if (fd == -1)
    {
        printf("error\n");
        return 2 ;
    }

    {
       int l;
       char buf[4096];
       struct iovec v[2];
       v[0].iov_base = &l;
       v[0].iov_len = sizeof(l);
       v[1].iov_base = buf;
       v[1].iov_len = sizeof(buf) - 1;
       ssize_t r = readv (fd, v, 2);

       if (r == -1)
       {
           printf("error\n");
           return 2;
       }

       buf[l] = 0;
       printf("readv text: \"%s\"\n", buf);
    }

    return 0;
}
